/**
 * 商品信息管理初始化
 */
var Goods = {
    id: "GoodsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    good_id: "",
    switchs: {}
};

/**
 * 初始化表格的列
 */
Goods.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '商品id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {
            title: '商品图片',
            field: 'goods_default_icon',
            visible: false,
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '<img width="100px" height="60px" src="' + Feng.ctxPath + '/kaptcha/' + value + '"/>'
            }
        },
        {title: '商品名称', field: 'goods_desc', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '商品价格', field: 'goods_default_price', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '销售数量', field: 'goods_sales_count', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '商品库存', field: 'goods_stock_count', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '商品编号', field: 'goods_code', visible: true, align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'state', visible: true, align: 'center', valign: 'middle'},
        {
            title: '操作', visible: true, align: 'center', valign: 'middle', formatter: function (value, row, index) {
                var id = row.id;
                return '<button type="button" class="btn btn-info  button-margin" onclick="Goods.goodsDetail(' + id + ')" id=""><i class="fa fa-search"></i></button>' +
                    '<button type="button" class="btn btn-primary button-margin" onclick="Goods.openGoodsDetail(' + id + ')" id=""><i class="fa fa-edit"></i></button>' +
                    '<button type="button" class="btn btn-danger button-margin" onclick="Goods.delete(' + id + ')" id=""><i class="fa fa-trash-o"></i></button>';

            }
        }
    ];
};



/**
 * 检查是否选中
 */
Goods.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Goods.seItem = selected[0];
        return true;
    }
};

Goods.hideImg = function (id) {
    $('#' + id).css('display', 'none');
};

Goods.showImg = function (id) {
    $('#' + id).css('display', 'block');
};

/**
 * 导出Excel
 */
Goods.getExcel = function () {
    var index = layer.open({
        type: 2,
        title: '导出商品信息配置',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/goods/selectExcel'
    });
    layer.full(index);
}
/**
 * 导入Excel
 */
Goods.setExcel = function () {
    var index = layer.open({
        type: 2,
        title: '导入商品信息配置',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/goods/setExcel'
    });
    layer.full(index);
}

/**
 * 点击添加商品信息
 */
Goods.openAddGoods = function () {
    var index = layer.open({
        type: 2,
        title: '添加商品信息',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/goods/goods_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看商品信息详情
 */
Goods.openGoodsDetail = function (good_id) {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '修改商品信息',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/goods/goods_update/' + good_id
        });
        this.layerIndex = index;
    }
};

/**
 * 查看商品信息详情
 */
Goods.goodsDetail = function (good_id) {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '商品信息详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/goods/detail/' + good_id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除商品信息
 */
Goods.delete = function (good_id) {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/goods/delete", function (data) {
            Feng.success("删除成功!");
            Goods.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("goodsId", good_id);
        ajax.start();
    }
};


/**
 * 查询商品信息列表
 */
Goods.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Goods.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Goods.initColumn();
    var table = new BSTable(Goods.id, "/product/list", defaultColunms);
    table.setPaginationType("client");
    Goods.table = table.init();


});
/**
 * 商品信息
 */
Goods.openGoods = function () {
    var index = layer.open({
        type: 2,
        title: '商品图片信息',
        area: ['300px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
    });
    this.layerIndex = index;
};